/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 package com.googlecode.milib.tools {
	import com.googlecode.milib.base.IMLBroadcaster;
	import com.googlecode.milib.base.IMLBroadcasterListener;
	import com.googlecode.milib.base.MLBroadcastClass;
	import com.googlecode.milib.data.IMLCollection;
	import com.googlecode.milib.data.MLCollection;
	import com.googlecode.milib.data.WeakReferencesCollection;
	import com.googlecode.milib.vo.MLEventInfo;

	/**
	 * @author Marek Brun
	 */
	public class MLEventsReflector extends MLBroadcastClass implements IMLBroadcasterListener {
		
		//DATA:	MLEventInfo //"real" event info
		public const event_EventReflection : String = 'EventReflection';
		
		private var objects:IMLCollection;
		
		public function MLEventsReflector(weakReferences:Boolean) {
			if(weakReferences){
				objects=new WeakReferencesCollection(null, null, IMLBroadcaster);
			}else{
				objects=new MLCollection(null, null, IMLBroadcaster);
			}
			
		}
		
		public function addObject(obj:IMLBroadcaster, $data:*=null):void {
			objects.add(obj, $data);
			obj.addListener(this);
		}
		
		public function addObjects(objects:Array):void {
			var i:uint;
			var loopObject:IMLBroadcaster;
			for(i=0;i<objects.length;i++){
				loopObject=objects[i];
				addObject(loopObject);
			}
		}
		
		public function removeObject(obj:IMLBroadcaster):void {
			objects.remove(obj);
			obj.removeListener(this);
		}
		
		public function clear():void {
			var objectsArr:Array=objects.toArray();
			var i:uint;
			var loopObjectsArr:IMLBroadcaster;
			for(i=0;i<objectsArr.length;i++){
				loopObjectsArr=objectsArr[i];
				loopObjectsArr.removeListener(this);
			}
			objects.clear();
		}
		
		public function getObjectByData(data:*):IMLBroadcaster {
			return objects.getValueByData(data);
		}
		
		public function getDataByObject(object:IMLBroadcaster):* {
			return objects.getDataByValue(object);
		}
		
		public function getAddedObjects():Array {
			return objects.toArray();
		}

		//********************************************************************************************
//		events for MLCollectionBroadcastReflector
//********************************************************************************************
		public function onEvent(ev:MLEventInfo):void {
			if(objects.contains(ev.hero)){
				broadcastEvent(event_EventReflection, ev);
			}
		}
		
	}
}
